<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span :style="comStyle">▎{{ getTitleName }}</span>
      <span
        class="iconfont icon-arrow-down icon"
        @click="showTitle = !showTitle"
        :style="comStyle"
      ></span>
      <div class="select-con" v-show="showTitle" :style="marginStyle">
        <div
          class="select-item"
          v-for="item in getTypeData"
          :key="item.key"
          @click="handleType(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trendRef"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";

export default {
  data() {
    return {
      myEchart: null,
      allData: null, //存放获取的所有数据
      showTitle: false, //显示与隐藏标题
      choiceType: "map", //当前选择的销量类型
      titleFontSize: 0, //标题的文字大小
    };
  },
  created() {
    this.$socket.registerCallBack("trendData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "trendData",
      chartName: "trend",
      value: "",
    });
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timer);
    this.$socket.unRegisterCallBack("trendData");
  },
  computed: {
    ...mapState(["theme"]),
    //计算当前所选择的销量类型
    getTypeData() {
      return this.allData
        ? this.allData.type.filter((item) => item.key !== this.choiceType)
        : [];
    },
    //计算当前所选销量类型的名字
    getTitleName() {
      return this.allData ? this.allData[this.choiceType].title : "";
    },
    //设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize * 1.5 + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize / 1.2 + "px",
      };
    },
  },

  watch: {
    theme() {
      //销毁当前图表
      this.myEchart.dispose();
      //以最新的主题初始化图表对象
      this.initChart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    /********************初始化**************************/
    initChart() {
      this.myEchart = this.$echarts.init(this.$refs.trendRef, this.theme);
      const initOption = {
        grid: {
          left: "3%",
          right: "4%",
          top: "35%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis", //鼠标移入坐标轴显示提示
        },
        //设置图例
        legend: {
          left: "center",
          top: "18%",
          icon: "circle", //图例icon的类型
        },
        xAxis: {
          type: "category",
          boundaryGap: false, //x轴紧挨边缘
        },
        yAxis: {
          type: "value",
        },
      };
      this.myEchart.setOption(initOption);
    },
    /********************调用接口获取数据**************************/
    getData(res) {
      // const { data: res } = await this.$http.get("trend");
      // console.log(res);
      this.allData = res;
      this.updateChart();
    },
    /********************更新数据**************************/
    updateChart() {
      //准备渐变颜色
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];
      //处理数据
      //x轴的数据----》时间
      const timeArr = this.allData.common.month;
      //y轴的数据--------》商品 地区 商家销量趋势
      const seriesArr = this.allData[this.choiceType].data.map(
        (item, index) => {
          return {
            name: item.name,
            type: "line",
            data: item.data,
            stack: "map",
            areaStyle: {
              // color:new this.$echarts.grahic.LinearGradient(x1,y1,x2,y2,[])
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: colorArr1[index],
                },
                {
                  offset: 1,
                  color: colorArr2[index],
                },
              ]),
            },
          };
        }
      );

      const dataOption = {
        xAxis: {
          data: timeArr,
        },

        series: seriesArr,
      };
      this.myEchart.setOption(dataOption);
    },
    /********************屏幕适配**************************/
    screenAdapter() {
      this.titleFontSize = (this.$refs.trendRef.offsetWidth / 100) * 3.2;
      const adaptOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          //间距
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
      };
      this.myEchart.setOption(adaptOption);
      this.myEchart.resize();
    },
    /********************点击销量趋势处理事件**************************/
    handleType(currentType) {
      this.choiceType = currentType;
      this.showTitle = false;
      this.updateChart();
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
  color: #fff;
  .icon {
    margin-left: 10px;
    cursor: pointer;
  }
}
</style>